<?php
require_once '/db2.php';
class Works2 extends DB {

    private $_table_name = 'works';
    private $_query = '';

    public function __construct() {
        $this->getConnection();
    }

    public function insertFromArray($works) {
             $ids=array();
             foreach ($works as $index=>$work) { 
                 $flag=$this->insert($work);
                        if(!empty($flag))
                            $ids[]=$flag;
             }
        return $ids;
    }

    function isEmpty($Works) {
        $warm = 0;
        foreach ($Works as $item => $value) {
            if (empty($value)) {
                $warm = 1;
                break;
            }
        }
        if ($warm == 1)
            echo "<h3 style='text-align:center'>Warm : </h3>";
        foreach ($Works as $item => $value) {
            if (empty($value))
                echo "<div class='notice'><b>Note:</b> " . $item . " Empty !</div>";
        }
    }

    public function insert($work) {
        /** works = array(
         * 'name',
         * 'description',
         * 'experience_requirements',
         * 'posted_date',
         * 'company_name',
         * 'company_address',
         * 'company_profile',
         * 'url'
         * )
         */
        $flag = $this->hasWorks($work['url']);
        if (empty($flag)) {
            $query = 'INSERT INTO ' . $this->_table_name . '(
                                        name,
                                        url,
                                        posted_date,
                                        location,
                                        description,
                                        experience_requirements,
                                        company_name,
                                        company_address,
                                        company_profile,
                                        id_site
                                        )' .
                    'VALUES(' .
                    '"' . mysql_escape_string($work['name']) . '",' .
                    '"' . mysql_escape_string($work['url']) . '",' .
                    $work['posted_date'] . ',' .
                    '"' . mysql_escape_string($work['location']) . '",' .
                    '"' . mysql_escape_string($work['description']) . '",' .
                    '"' . mysql_escape_string($work['experience_requirements']) . '",' .
                    '"' . mysql_escape_string($work['company_name']) . '",' .
                    '"' . mysql_escape_string($work['company_address']) . '",' .
                    '"' . mysql_escape_string($work['company_profile']) . '",'.
                    '"' . mysql_escape_string($work['id_site']). '"'.
                    ')';
           // $this->isEmpty($work);
            $this->query($query);
            return mysql_insert_id();
        } else {
            return NULL;
        }
    }

    public function hasWorks($url) {
        $query = 'SELECT url FROM ' . $this->_table_name . '
                  WHERE url like "' . mysql_real_escape_string($url) . '"';
        $results = $this->query($query);
        $row = mysql_fetch_assoc($results);
        return $row;
    }

    public function select($params = NULL, $page = NULL,$counter=FALSE) {
        $works = array();
        
        //Fields
        $fields = '*';
        if ($counter) {
            $fields = 'count(*) AS counter';
        }
        //Where
                $where = "WHERE (1=1)";
            //Where site;
                if (!empty($params['id_site'])) {
                    $where .= " AND (id_site = {$params['id_site']})";
                }
            //Where keyword
                if (!empty($params['keyword'])) {
                    $where .= " AND (description LIKE '% {$params['keyword']} %')";
                }
           //Where listId_works;
                if (!empty($params['listId_works'])) {
                    $where .= ' AND id IN ('.$params['listId_works'].')';
                }
        //Limit
        $limit = '';
        if (!empty($page)) {
            $start = ($page - 1) * PER_PAGE;
            $limit = "LIMIT $start,".PER_PAGE;
        } 
        if (empty($page)) {
            $page = 1;
        }
        
        //Query string
        $query = "SELECT $fields FROM $this->_table_name 
                  $where
                 ";
        //Query data
        $results = $this->query($query);
        while ($row = mysql_fetch_assoc($results)) {
            $works[] = $row;
        }
        return $works;
    }

    public function aSelect($id) {
        $query = 'SELECT * FROM ' . $this->_table_name . ' WHERE id=' . $id . ' ORDER BY id DESC';
        $results = $this->query($query);
        $row = mysql_fetch_assoc($results);
        return $row;
    }

    public function updates($work = array()) {
        $query = "UPDATE " . $this->_table_name . "
                                    SET
                                         id=" . $work['id'] . ",
                                         name='" . $work['name'] . "',
                                         url='" . $work['url'] . "',
                                         location='" . $work['location'] . "',
                                         company_name='" . $work['company_name'] . "',
                                         company_address='" . $work['company_address'] . "',
                                         company_profile='" . $work['company_profile'] . "',
                                         description='" . $work['description'] . "',
                                         experience_requirements='" . $work['experience_requirements'] . "'
                 WHERE id=" . $work['id'];
        return $this->query($query);
    }

    public function deleteWorks($id) {
        $query = "DELETE FROM $this->_table_name WHERE id=$id";
        return $this->query($query);
    }

}